package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.OrganismImage;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QOrganismImage;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = OrganismImageRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = OrganismImageRepositoryCustom.class)
@Transactional(readOnly = true)
public class OrganismImageRepositoryImpl extends QueryDslRepositorySupportExt<OrganismImage> implements OrganismImageRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    OrganismImageRepositoryImpl() {
        super(OrganismImage.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LOCATION = "location";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CODE = "code";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATE_DATE = "createDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String AUTHOR = "author";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String URL = "url";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<OrganismImage> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QOrganismImage organismImage = QOrganismImage.organismImage;
        JPQLQuery<OrganismImage> query = from(organismImage);
        Assert.notNull(organism, "organism is required");
        query.where(organismImage.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { organismImage.organism, organismImage.code, organismImage.description, organismImage.location, organismImage.author, organismImage.url, organismImage.createDate };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, organismImage.organism).map(CODE, organismImage.code).map(DESCRIPTION, organismImage.description).map(LOCATION, organismImage.location).map(AUTHOR, organismImage.author).map(URL, organismImage.url).map(CREATE_DATE, organismImage.createDate);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organismImage);
    }
}
